<?php

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Session;
use app\admin\model\Goods as GoodsModel;

class Goods extends AdminController
{
    //礼品列表
    public function index()
    {
        $res = GoodsModel::select();
        return $this->assign('res',$res)->fetch('index');
    }

    //礼品详情页
    public function info($id)
    {
        $res = Db::table('xt_goods_property')->where('gid',$id)->select();
        return $this->assign('res',$res)->fetch('info');
    }

    //礼品添加页
    public function create()
    {
        return $this->fetch('add');
    }

    //礼品编辑页
    public function edit($id)
    {
        $goods = Db::table('xt_goods')->where('id',$id)->find();
        $pro = Db::table('xt_goods_property')->where('gid',$id)->select();
        foreach ($pro as $key => $val) {
            $proName = $val['proName'];
        }
        $this->assign(['id' => $id,'proName' => $proName]);
        // 变量赋值, 模板渲染
        return $this->assign(['goods'=>$goods,'pro'=>$pro])->fetch('edit');
    }
     //执行礼品编辑
    public function update(Request $request,$id)
    {
        $res = $request->post();

        if( empty($res['goodsName']) ){
            $this->error('商品名不能为空'); die;
        }
        if( empty($res['description']) ){
            $this->error('商品描述不能为空'); die;
        }
        $data = array();
        // 商品封面上传
        $file = $request->file("goodsImg");
        // var_dump($file);die;
        if ( $file ) {
            //移动到框架应用根目录/public/uploads/goodsimg 目录下
            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/goodsimg');
            if($info){
                // 变化过的文件名称
                $img = $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                $title = $file->getError();
                $this->error($title); die;
            }
            $data['goodsImg'] = $img;
        }

        $data['goodsName'] = $res['goodsName'];
        $data['description'] = $res['description'];
        $result = Db::table('xt_goods')->where('id',$id)->update($data);
        // $result = true;
        if( $result ){

            $prodata = array();
            //商品封面上传
            $fileArr = $request->file("proImg");
            $img = array();
            if ( is_array($fileArr)) {
               foreach ($fileArr as $key => $file) {
                    //移动到框架应用根目录/public/uploads/goodsimg 目录下
                    $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/goodsimg');
                    if($info){
                        // 变化过的文件名称
                        $img[] = $info->getSaveName();
                        // var_dump($img);die;
                    }else{
                        // 上传失败获取错误信息
                        $title = $file->getError();
                        $this->error($title); die;
                    }
                }
                $prodata['proImg'] = $img;
            }
            
            $prodata['property'] = $res['property'];
            $prodata['price'] = $res['price'];
            $prodata['marketPrice'] = $res['marketPrice'];
            $prodata['store'] = $res['store'];
            $prodata['sales'] = $res['sales'];
            // var_dump($prodata);die;
            $proArr = array();
            for ( $i = 0; $i < count($prodata['property']); $i++)
            {
                foreach( $prodata as $key => $val){
                    $proArr[$i][$key] = $val[$i];
                    $proArr[$i]['proName'] = $res['proName'];
                    $proArr[$i]['gid'] = $id;
                    $proArr[$i]['putDate'] = date('Y-m-d H:i:s');
                }
            }
            // var_dump($proArr);die;
            $delPro = Db::table('xt_goods_property')->where('gid',$id)->delete();

            foreach($proArr as $v){
                $resPro = Db::table('xt_goods_property')->insert($v);
            }

            if ( $resPro ) {
                $this->success('商品修改成功'); die;
            }else{
                $this->error('商品修改失败'); die;
            }

        }else{
            $this->success('商品修改失败'); die;
        }

    }

    //执行礼品添加
    public function save()
    {
        $request = Request::instance();
        $res = $request->post();

        if(!$res['goodsName'] ){
            $this->error('商品名不能为空'); die;
        }
        if(!$res['description'] ){
            $this->error('商品描述不能为空'); die;
        }
        $data = array();
         // 商品封面上传
        $file = $request->file("goodsImg");
        // var_dump($file);die;
        if ( $file ) {
            //移动到框架应用根目录/public/uploads/goodsimg 目录下
            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/goodsimg');
            if($info){
                // 变化过的文件名称
                $img = $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                $title = $file->getError();
                $this->error($title); die;
            }
            $data['goodsImg'] = $img;
        }

        $data['goodsName'] = $res['goodsName'];
        $data['description'] = $res['description'];
        $result = Db::table('xt_goods')->insert($data);
        $gid = GoodsModel::getLastInsId();

        if ( $result ) {
            $prodata = array();
            //商品封面上传
            $fileArr = $request->file("proImg");
            $imgs = array();
            if ( is_array($fileArr)) {
               foreach ($fileArr as $key => $file) {
                    //移动到框架应用根目录/public/uploads/goodsimg 目录下
                    $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/goodsimg');
                    if($info){
                        // 变化过的文件名称
                        $imgs[] = $info->getSaveName();
                        // var_dump($img);die;
                    }else{
                        // 上传失败获取错误信息
                        $title = $file->getError();
                        $this->error($title); die;
                    }
                }
                $prodata['proImg'] = $imgs;
            }

            $prodata['property'] = $res['property'];
            $prodata['price'] = $res['price'];
            $prodata['marketPrice'] = $res['marketPrice'];
            $prodata['store'] = $res['store'];
            $prodata['sales'] = $res['sales'];

            $proArr = array();
            for ( $i = 0; $i < count($prodata['property']); $i++)
            {
                foreach( $prodata as $key => $val){
                    $proArr[$i][$key] = $val[$i];
                    $proArr[$i]['proName'] = $res['proName'];
                    $proArr[$i]['gid'] = $gid;
                    $proArr[$i]['putDate'] = date('Y-m-d H:i:s');
                }
            }

            foreach($proArr as $v){
                $resPro = Db::table('xt_goods_property')->insert($v);
            }

            if( $resPro ){
                $this->success('商品添加成功',url('admin/goods/index')); die;
            }else{
                $this->success('商品添加成功,规格新增失败'); die;
            }
        }else{
            $this->error('商品添加失败'); die;
        }
    }

    //礼品删除
    public function delete($id)
    {
        $res = Request::instance()->param();

        $result = GoodsModel::get($id)->delete();
        $res = Db::table('xt_goods_property')->where('gid', $id)->delete();


        if ($result > 0 && $res > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的商品删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的商品删除失败!';
        }
        return json($info);

    }


    //商品订单页
    public function order()
    {
        $res = Db::table('xt_goods_order gr')->join('xt_goods g', 'g.id = gr.gid')->select();
        $userName = session::get('userName');
        return $this->assign(['res'=>$res, 'userName'=>$userName])->fetch('order');
    }
    //订单详情
    public function orderinfo($id)
    {
        $res = Db::table('xt_goods g')->join('xt_goods_order go', 'g.id = go.gid')->where('g.id', $id)->find();
        return $this->assign('res',$res)->fetch('orderDetail');
    }

   

     //订单删除
    public function orderDel($id)
    {
        $result = Db::table('xt_goods_order')->where('gid', $id)->delete();

        if ($result > 0 ) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的订单删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的订单删除失败!';
        }
        return json($info);

    }

}
